﻿using DayuCode.Designer.Entity;
using DayuCode.Designer.Service;
using DayuCode.Designer.Service.ui;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms
{
    public partial class FormFuncConfig : UIForm
    {
        Project m_project;
        Func m_func;
        List<TemplatePageLayout> m_webPageLayouts = new List<TemplatePageLayout>();
        List<TemplatePageLayout> m_appPageLayouts = new List<TemplatePageLayout>();
        List<TemplatePageLayout> m_padPageLayouts = new List<TemplatePageLayout>();
        public FormFuncConfig()
        {
            InitializeComponent();

            funcTree.OnSelectedFuncChange += FuncTree_OnSelectedFuncChange;
            loadPageLayouts();

            txtFunName.FillColor = Color.FromArgb(235, 243, 255);
            txtTablesRelation.FillColor = Color.FromArgb(235, 243, 255);
            txtJavaPkg.FillColor = Color.FromArgb(235, 243, 255);
            txtWebPagelayout.FillColor = Color.FromArgb(235, 243, 255);
            txtAppPagelayout.FillColor = Color.FromArgb(235, 243, 255);
            txtPadPagelayout.FillColor = Color.FromArgb(235, 243, 255);
        }

        private void FormFuncConfig_Load(object sender, EventArgs e)
        {

        }

        public void SetProject(long projectId)
        {
            if (projectId == 0) return;
            m_project = ProjectHandler.getProject(projectId);
            this.Text = "功能配置 - [ 应用系统：" + m_project.Name + " ]";

            funcTree.SetCurrProject(projectId);
            
        }

        private void loadPageLayouts()
        {
            var webPageLayouts = TemplatePageLayoutHandler.getTemplatePageLayouts(TerminalType.WEB);
            var appPageLayouts = TemplatePageLayoutHandler.getTemplatePageLayouts(TerminalType.APP);
            var padPageLayouts = TemplatePageLayoutHandler.getTemplatePageLayouts(TerminalType.PAD);
            if (webPageLayouts != null)
            {
                m_webPageLayouts.AddRange(webPageLayouts);
            }
            if (appPageLayouts != null)
            {
                m_appPageLayouts.AddRange(appPageLayouts);
            }
            if (padPageLayouts != null)
            {
                m_padPageLayouts.AddRange(padPageLayouts);
            }
        }

        private void FuncTree_OnSelectedFuncChange(Func selectedFunc)
        {
            changeSelectedFunc(selectedFunc);
        }

        private void changeSelectedFunc(Func func)
        {
            m_func = func;
            if (func != null)
            {
                txtFunName.Text = func.Name;
                txtTablesRelation.Text = func.TablesRelationCaption;
                txtJavaPkg.Text = func.JavaPkg;

                txtWebPagelayout.Text = "";
                txtAppPagelayout.Text = "";
                txtPadPagelayout.Text = "";
                var webPageLayout = m_webPageLayouts.Find(it => it.Id == func.WebPageLayoutId);
                var appPageLayout = m_appPageLayouts.Find(it => it.Id == func.AppPageLayoutId);
                var padPageLayout = m_padPageLayouts.Find(it => it.Id == func.PadPageLayoutId);
                if (webPageLayout != null) txtWebPagelayout.Text = webPageLayout.LayoutName;
                if (appPageLayout != null) txtAppPagelayout.Text = appPageLayout.LayoutName;
                if (padPageLayout != null) txtPadPagelayout.Text = padPageLayout.LayoutName;

                formTableUIConfig.SetFunc(func.ProjectId, func.Id);            
            }
        }

        private void FormFuncConfig_Shown(object sender, EventArgs e)
        {
           
        }

        private void btnSavePageConfig_Click(object sender, EventArgs e)
        {
            if (m_project == null || m_func == null) return;
            System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;
            try
            {
                if (formTableUIConfig.SavePageConfig(true))
                {
                    this.ShowSuccessTip("配置保存成功");
                }
            }
            finally
            {
                System.Windows.Forms.Cursor.Current = Cursors.Default;
            }
        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            if (m_project == null || m_func == null) return;
            System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;
            try
            {
                if (formTableUIConfig.SavePageConfig(false))
                {
                    string terminalType = formTableUIConfig.GetTerminalType();
                    FormSourceCode form = new FormSourceCode(m_project.Id, m_func.Id, terminalType);
                    form.Owner = this;
                    form.ShowDialog();
                }
            }
            finally
            {
                System.Windows.Forms.Cursor.Current = Cursors.Default;
            }
        }
    }
}
